<?php
/**
 * 地区类
 *
 * @version 	0.0.1
 */

class Districtmodel extends Model 
{
	
	// ------------------------------------------------------------------------
	/**
	 * initialises the class inheriting the methods of the class Model 
	 *
	 * @return Systemparam
	 */
    function Districtmodel()
    {
        parent::Model();
    }
	
    // ------------------------------------------------------------------------
    
    // 根据ID获取地区记录，返回记录集
    function getDistrictById($id)
    {
        //$this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            return $query->row();
        }
    	return ;
    }

    // 根据层级获取地区信息
    function getDistrictByLevel($level)
    {
        $this->db->where('DIS_LEVEL', $level);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    	return ;
    }

    // 根据上级ID获取地区信息
    function getDistrictByPid($pid)
    {
        $this->db->where('PID', $pid);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    	return ;
    }

    // 根据上级名字获取地区信息
    function getDistrictByPname($pname)
    {
        $this->db->where('NAME', $pname);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            return $this->getDistrictByPid($row->ID);
        }
    	return ;
    }

    // 根据地区名字获取地区ID，为防止不同层次的地区名字重复，进行严格层次限制
    function getDistrictIdByName($pid, $name)
    {
        if (!empty($pid))
        {
        	$this->db->where('PID', $pid);
        }
        $this->db->where('NAME', $name);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            return $row->ID;
        }
    	return 0;
    }

    // 根据顶级地区ID获取下级所有地区信息
    function getAllDistrictById($id)
    {
		$where = "pid in (select id from tb_district where pid='$id')";
		$this->db->where($where);
        $query = $this->db->get('district');
        
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    	return ;
    }


	// 根据地区上级ID查询地区信息，返回XML
	function getDistrictXml($pid)
	{
		$this->load->helper('xml');

		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		$output = '<?xml version="1.0" encoding="UTF-8" ?>';
		$data = $this->getDistrictByPid($pid);
		$output .= "<DistrictReponse>";
		if (!empty($data))
		{
			foreach ($data as $item)
			{
				$output .= "<Items>\n";
				$output .= "<Item>\n";
				foreach ($item as $key => $value)
				{
					$output .= "<$key>".xml_convert($value)."</$key>\n";
				}
				$output .= "</Item>\n";
				$output .= "</Items>";
			}
		}
		$output .= '</DistrictReponse>';

		return $output;
	}

	// 根据顶级ID查询下级所有地区信息，返回XML
	function getAllDistrictXml($id)
	{
		$this->load->helper('xml');

		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
		
		$output = '<?xml version="1.0" encoding="UTF-8" ?>';
		$data = $this->getAllDistrictById($id);
		$output .= "<DistrictReponse>";
		if (!empty($data))
		{
			foreach ($data as $item)
			{
				$output .= "<Items>\n";
				$output .= "<Item>\n";
				foreach ($item as $key => $value)
				{
					$output .= "<$key>".xml_convert($value)."</$key>\n";
				}
				$output .= "</Item>\n";
				$output .= "</Items>";
			}
		}
		$output .= '</DistrictReponse>';

		return $output;
	}

}